package jmg

import (
	"lol.com/feb214-game/nest.git/tools/sample"
	"math/rand"
)

const (
	//card相关(炸金花不包含大小王)
	MaxCard      = 51 //最大点数的牌, 黑桃K
	MaxGroupCard = 8  //8副牌共416张
)

//Shuffle 洗牌, 随机抽牌插底 O(n)
func shuffle(cards []int32) {
	for len(cards) > 0 {
		n := len(cards)
		randIndex := rand.Intn(n)
		cards[n-1], cards[randIndex] = cards[randIndex], cards[n-1]
		cards = cards[:n-1]
	}
}

// DispatchCards 发牌
// playerCount: 玩家人数
// 返回玩家手牌列表和剩余卡牌
func DispatchCards(cards []int32, playerCount int) ([]int32, []int32) {
	sample.ShuffleInt32(cards)
	var playCards = make([]int32, playerCount)
	if playerCount > 0 {
		copy(playCards, cards[:playerCount])
	}
	cards = cards[playerCount:]
	return playCards, cards
}

// InitCards 根据参与人数初始化牌组，返回每组牌和剩余牌堆
func InitCards(playerCount int, cardsCutIndexes []int) ([]int32, []int32) {
	//初始化牌
	cards := make([]int32, 0, totalCards)
	for g := int32(0); g < MaxGroupCard; g++ {
		for i := int32(0); i <= MaxCard; i++ {
			cards = append(cards, i)
		}
	}
	//洗牌
	shuffle(cards)
	//抬牌, cardsCutIndex前面的牌丢弃
	cardsCutIndex := rand.Intn(cardsCutIndexes[1] - cardsCutIndexes[0])
	cardsCutIndex = cardsCutIndex + cardsCutIndexes[0]
	cards = cards[cardsCutIndex:]
	//发牌
	return DispatchCards(cards, playerCount)
}
